package com.java.zhuiyun.api.rolerank.service.impl;

import com.java.zhuiyun.api.playerinformation.entity.PlayerInformation;
import com.java.zhuiyun.api.playerinformation.mapper.PlayerInformationMapper;
import com.java.zhuiyun.api.rolerank.entity.RoleRank;
import com.java.zhuiyun.api.rolerank.mapper.RolerankMapper;
import com.java.zhuiyun.api.rolerank.model.Modelsize;
import com.java.zhuiyun.api.rolerank.service.RolerankService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.UploadFileUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @ClassName RolerankServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/9/13 19:41
 **/
@Service
public class RolerankServiceImpl implements RolerankService {
    @Autowired
    private RolerankMapper rolerankMapper;
    @Autowired
    private PlayerInformationMapper playerInformationMapper;
    @Override
    public CommonResult selectRolerank(PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(rolerankMapper.selectRolerank());
        return CommonResult.ok(pageResult);
    }

    @Override
    @Transactional
    public CommonResult addRolerank(MultipartFile file,RoleRank roleRank) {
        String s = UploadFileUtils.uploadFile(file);
        roleRank.setRoleImg(Integer.parseInt(s));
        Modelsize modelsize = rolerankMapper.selectRolerankCount();
        roleRank.setRoleLevel(modelsize.getSizes()+1);
        roleRank.setCreateTime(new Date());
        roleRank.setUpdateTime(new Date());
        Integer integer = rolerankMapper.addRolerank(roleRank);
        if (integer >= 1) {
            return CommonResult.ok("角色添加成功");
        }
        return CommonResult.error("角色添加失败");
    }

    @Override
    @Transactional
    public CommonResult updateRolerank(MultipartFile file, RoleRank roleRank) {
        if (file == null){
            roleRank.setUpdateTime(new Date());
            Integer integer = rolerankMapper.updateRoleranks(roleRank);
            if (integer >= 1) {
                return CommonResult.ok("道具修改成功");
            }
            return CommonResult.error("道具修改失败");
        }
        String s = UploadFileUtils.uploadFile(file);
        roleRank.setRoleImg(Integer.parseInt(s));
        roleRank.setUpdateTime(new Date());
        Integer integer = rolerankMapper.updateRolerank(roleRank);
        if (integer >= 1) {
            return CommonResult.ok("道具修改成功");
        }
        return CommonResult.error("道具修改失败");
    }

    @Override
    public CommonResult getRoleData(String openid) {
        PlayerInformation playerInformation = playerInformationMapper.selectPlayerLevel(openid);
        Integer currentPlayerLevel = playerInformation.getCurrentPlayerLevel();
        return CommonResult.ok(rolerankMapper.getRoleData(currentPlayerLevel));
    }

}
